package com.online.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.github.pagehelper.Page;
import com.online.pojo.Role;
import com.online.user.service.RoleService;
import com.online.user.mapper.RoleMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;


import java.util.List;

/**
* @author 瑾
* @description 针对表【on_role】的数据库操作Service实现
* @createDate 2025-01-26 18:53:58
*/
@Service
public class RoleServiceImpl implements RoleService{
    @Autowired
    private RoleMapper iRoleMapper;

    @Override
    public List<Role> selectAll() {
        LambdaQueryWrapper <Role> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Role::getIsDelete,0);
        return iRoleMapper.selectList(lqw);
    }


    @Override
    public List<Role> selectList(Role iRole) {
        LambdaQueryWrapper <Role> lqw = this.lqw(iRole);
        return iRoleMapper.selectList(lqw);
    }

    @Override
    public Integer insert(Role iRole) {
        return iRoleMapper.insert(iRole);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                Role iRole = new Role();
                iRole.setId(Integer.valueOf(id));
                iRole.setIsDelete(1);
                iRoleMapper.updateById(iRole);
            }
            return 1;
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public Integer update(Role iRole) {
        return iRoleMapper.updateById(iRole);
    }

    @Override
    public Page<Role> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<Role>)this.selectAll();
    }

    @Override
    public Page<Role> pageList(Role iRole, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<Role>)this.selectList(iRole);
    }

    public LambdaQueryWrapper lqw(Role iRole){
        LambdaQueryWrapper <Role> lqw = new LambdaQueryWrapper<>();
        if (iRole.getId() != null){
            lqw.eq(Role::getId,iRole.getId());
        }
        if (iRole.getRole() != null){
            lqw.eq(Role::getRole,iRole.getRole());
        }
        return lqw;
    }
}




